package com.netty.bfport.http.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netty.bfport.http.common.base.BaseResponse;
import com.netty.bfport.http.common.base.BaseResponseVO;
import com.netty.bfport.http.common.base.BaseStatus;
import com.netty.bfport.http.common.utils.AesEncodeUtil;
import com.netty.bfport.http.domain.dto.UserLoginDTO;
import com.netty.bfport.http.service.UserService;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.netty.bfport.http.common.base.BaseStaticMsg.TOKEN;

/**
 * @Author: zhaoyang05
 * @Data: 2022/3/29 21:35
 * @Description
 */
@RestController
public class UserLoginController {
    @Resource
    UserService userService;

    @Resource
    RedisTemplate<String,Object> redisTemplate;


    /**
     * 验证token
     * */
    @PostMapping("/token/analysis")
    public BaseResponseVO tokenAnalysis(HttpServletRequest request){
        String token = request.getHeader("token");
        String s = StringEscapeUtils.unescapeJava(token);
        //转义/r/n
        JSONObject msg = userService.tokenAnalysis(s);
        return msg == null? BaseResponse.setResultError(BaseStatus.TOKEN_ERROR,"token错误"): BaseResponse.setResultSuccess("token正确");
    }

    /**
     * 密码登录
     * */
    @PostMapping("/login")
    public BaseResponseVO userLogin(@Validated @RequestBody UserLoginDTO userDTO, HttpServletRequest request){
        JSONObject token = userService.loginPas(userDTO, request);
//        Map<String,String> map = new HashMap<>();
//        map.put("token",token);
        return token.getString("token").equals("0")?BaseResponse.setResultError(BaseStatus.USERPASSWORD_ERROR,"账号或密码错误"):BaseResponse.setResultSuccess(token);
    }

    /**
     * 注册
     * */
    @PostMapping("/sign")
    public BaseResponseVO userSign(@Validated @RequestBody UserLoginDTO userDTO, HttpServletRequest request){
        JSONObject sign = userService.sign(userDTO, request);
        switch (sign.getString("token")){
            case "0":
                return BaseResponse.setResultError(BaseStatus.SHORT_CODE_ERROR,"验证码错误");
            case "1":
                return BaseResponse.setResultError("添加失败");
            default:
                return BaseResponse.setResultSuccess(sign);
        }

    }

    /**
     * 退出登录
     * */
    @DeleteMapping("/down")
    public BaseResponseVO userDown(HttpServletRequest request){
        String token = AesEncodeUtil.decrypt(request.getHeader("token"));
        redisTemplate.delete(TOKEN+token);
        return BaseResponse.setResultSuccess("退出成功");
    }
}
